//
//  KRLTypeView.m
//  KaraLive
//
//  Created by Zhijia Zhong on 2018/9/15.
//  Copyright © 2018年 naxigoren. All rights reserved.
//

#import "KRLTypeView.h"

@interface KRLTypeView()

@property (nonatomic, strong) UIImageView *videoView;
@property (nonatomic, strong) UILabel *typeLabel;

@end

@implementation KRLTypeView

- (UIImageView *)videoView {
    if (!_videoView) {
        _videoView = [[UIImageView alloc] init];
        _videoView.image = [UIImage imageNamed:@"icon_record"];
    }
    return _videoView;
}

- (UILabel *)typeLabel {
    if (!_typeLabel) {
        _typeLabel = [[UILabel alloc] init];
        _typeLabel.textColor = [UIColor whiteColor];
        _typeLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium];
    }
    return _typeLabel;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.translatesAutoresizingMaskIntoConstraints = NO;
        
        self.colors = @[[UIColor colorWithRGB:0xff6c6c], [UIColor colorWithRGB:0xff9451]];
        self.startPoint = CGPointMake(0, 0);
        self.endPoint = CGPointMake(1, 0);
        
        [self addSubview:self.videoView];
        [self addSubview:self.typeLabel];

        [self configLayer];
    }
    return self;
}

- (void)setBounds:(CGRect)bounds {
    [super setBounds:bounds];
    
    CGFloat radius = self.height / 2;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *layer = (CAShapeLayer *)self.layer.mask;
    layer.path = maskPath.CGPath;
    self.layer.mask.frame = self.bounds;
}

- (void)configLayer {
    CAGradientLayer *gradientLayer = (CAGradientLayer *)self.layer;
    
    // 添加mask，用于实现圆角
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    gradientLayer.mask = maskLayer;
}

+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}

- (void)updateConstraints {
    [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8.5);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(11.5, 6));
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(self.videoView.mas_right).offset(3.5);
        make.right.mas_equalTo(-7);
    }];

    [super updateConstraints];
}

- (void)setType:(KRLTimelineType)type {
    switch (type) {
        case KRLTimelineTypeKTV:
            self.typeLabel.text = @"KTV";
            break;
        case KRLTimelineTypeRecord:
            self.typeLabel.text = @"RECORD";
            break;
        case KRLTimelineTypeVLog:
            self.typeLabel.text = @"VLOG";
            break;
        default:
            self.typeLabel.text = @"";
            break;
    }
    [self.typeLabel sizeToFit];
}

@end
